/* jshint esversion:6 */
/**
 * @author: errorcpp@qq.com
 */

var xwindow = ("undefined" != typeof(window)) ? window : global;
xwindow.xlib = xwindow.xlib || {};
let xlib = xwindow.xlib;

require("./xlib_base");
require("./xlib_utf8");

/**
 * @brief: 填充array
 */
xlib.Array_FillN = function (arr, def_val, count) {
    for(let n = 0; n < count; ++n) {
        arr[n] = def_val;
    }

    arr.length = count;
};

/**
 * @brief: 使用utf8字符串比特数据填充数组
 *    本函数只保证数据不越界，不对末尾结束标记负责
 * @param {Array} arr: 目标数组
 * @param {String} str: 源字符串
 */
xlib.Array_FillWithUtf8Byte = function (arr, str) {
    let arr_tem = xlib.Utf8.StringToBytes(str);

    xlib.Array_Copy(arr_tem, 0, arr_tem.length, arr, 0);
};

/**
 * @brief: 数组拷贝
 * @param {Array} arr_src:    源数组
 * @param {number} begin_src: 开始拷贝的位置
 * @param {number} end_src:   结束拷贝的位置
 * @param {Array} arr_tar:    目标数组
 * @param {number} begin_tar: 目标数组开始接收的位置
 */
xlib.Array_Copy = function (arr_src, begin_src, end_src, arr_tar, begin_tar) {
    if(!xlib.IsValid(begin_tar)) begin_tar = 0;
    if((arr_src.length < end_src) ||
       (end_src < begin_src) ||
       (arr_tar.length < begin_tar + (end_src - begin_src))) {
        throw "out of range !";       
    }

    let count_need_copy = end_src - begin_src;
    for(let n = 0; n < count_need_copy; ++n) {
        arr_src[begin_src + n] = arr_tar[begin_tar + n];
    }
};

/**
 * @brief: U8比特数组数组转字符串
 * @param {Array} arr: 数组
 * @return {String}: 字符串
 */
xlib.Array_BuildStringFromU8Array = function (arr) {
    let tem = xlib.Utf8.BytesToString(arr);

    return tem;
};

/**
 * @brief: 从字符串对象转UTF8比特数组
 * @param {String} str: 源字符串
 */
xlib.Array_BuildU8ArrayFromString = function (str) {
    let tem = xlib.Utf8.StringToBytes(str);

    return tem;
};

